//
//  ThemeAlertView.m
//  DTSampleLight
//
//  Created by ZhangYeKun on 2017/3/24.
//  Copyright © 2017年 Fidetro. All rights reserved.
//

#import "ThemeAlertView.h"

@interface ThemeAlertView ()
/** 背景 **/
@property(nonatomic,strong) UIView *bgView;
/** 标题 **/
@property(nonatomic,strong) UILabel *titleLabel;

/** 弹窗类型 **/
@property(nonatomic,assign) NSInteger type; // 0为默认 1为有图片

@end

@implementation ThemeAlertView

- (instancetype)initWithType:(NSInteger)type{
    
    if ([super init]) {
        self.type = type;
        [self masLayoutSubview];
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        
    }
    
    return self;
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}
/**
 取消按钮点击事件
 */
- (void)cancelAction:(UIButton *)sender
{
    [self calloutAnimation];
    if (self.cancel_block) {
        self.cancel_block(sender);
    }
}

- (void)enterAction:(UIButton *)sender
{
    [self calloutAnimation];
    if (self.enter_block) {
        self.enter_block(sender);
    }
    else
    {
        [self calloutAnimation];
    }
}

- (void)callinAnimation
{
    [UIView transitionWithView:self duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    } completion:nil];
}

- (void)calloutAnimation
{
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}



- (UIView *)bgView
{
    if (!_bgView) {
        UIView *superView = self;
        _bgView = [[UIView alloc]init];
        [superView addSubview:_bgView];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.cornerRadius = 4.f;
    }
    return _bgView;
}

- (UIButton *)enterButton
{
    if (!_enterButton) {
        UIView *superView = self.bgView;
        _enterButton = [[UIButton alloc]init];
        [superView addSubview:_enterButton];
        [_enterButton setTitle:@"确认" forState:UIControlStateNormal];
        [_enterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_enterButton setBackgroundColor:[UIColor redColor]];
        [_enterButton addTarget:self action:@selector(enterAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _enterButton;
}

- (UIButton *)cancelButton
{
    if (!_cancelButton) {
        UIView *superView = self.bgView;
        _cancelButton = [[UIButton alloc]init];
        [superView addSubview:_cancelButton];
        [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_cancelButton setBackgroundImage:[UIImage imageNamed:@"del_bor"] forState:UIControlStateNormal];
        [_cancelButton setBackgroundColor:[UIColor blackColor]];
        [_cancelButton addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        UIView *superView = self.bgView;
        _titleLabel = [[UILabel alloc]init];
        [superView addSubview:_titleLabel];
        _titleLabel.text = @"——  提示  ——";
        [_titleLabel setFont:[UIFont systemFontOfSize:17]];
    }
    return _titleLabel;
}

- (UILabel *)textLabel {
    if (!_textLabel) {
        UIView *superView = self.bgView;
        _textLabel = [[UILabel alloc] init];
        [superView addSubview:_textLabel];
        _textLabel.font = [UIFont systemFontOfSize:13];
        _textLabel.numberOfLines = 0;
    }
    return _textLabel;
}

- (UITextField *)textField
{
    if (!_textField) {
        UIView *superView = self.bgView;
        _textField = [[UITextField alloc]init];
        [superView addSubview:_textField];
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.font = [UIFont systemFontOfSize:13];
//        _textField.layer.borderColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1].CGColor;
//        _textField.layer.borderWidth = 1;
        _textField.returnKeyType = UIReturnKeyDone;
        
        
    }
    return _textField;
}

- (UIImageView *)imageView
{
    if(!_imageView)
    {
        UIView *superView = self.bgView;
        _imageView = [[UIImageView alloc]init];
        [superView addSubview:_imageView];
    }
    return _imageView;
}


- (void)masLayoutSubview
{
    UIView *superView = self;
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.centerY.mas_equalTo(superView.mas_centerY);
        if (self.type == 1) {
            make.height.mas_equalTo(px1920Height(900));
        } else {
            make.height.mas_equalTo(px1920Height(497));
        }
        make.width.mas_equalTo(px1080Width(900));
    }];
    
    [self.enterButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bgView.mas_left).mas_offset(px1080Width(72));
        make.right.mas_equalTo(self.bgView.mas_centerX).mas_offset(-px1080Width(14));
        make.height.mas_equalTo(px1920Height(130));
        make.bottom.mas_equalTo(self.bgView.mas_bottom).mas_offset(-px1920Height(49));
    }];
    [self.cancelButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.bgView.mas_right).mas_offset(-px1080Width(72));
        make.left.mas_equalTo(self.bgView.mas_centerX).mas_offset(px1080Width(14));
        make.height.mas_equalTo(px1920Height(130));
        make.bottom.mas_equalTo(self.bgView.mas_bottom).mas_offset(-px1920Height(49));
    }];
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgView.mas_top).mas_offset(px1920Height(55));
        make.centerX.mas_equalTo(self.bgView.mas_centerX);
    }];
    [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.bgView.mas_left).mas_offset(px1080Width(43));
        make.right.mas_equalTo(self.bgView.mas_right).mas_offset(-px1080Width(43));
        make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(px1920Height(60));
        make.height.mas_equalTo(px1920Height(120));
    }];
    
    if (self.type == 1) {
        self.textField.hidden = YES;
        
        [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titleLabel.mas_bottom ).mas_offset(20);
            make.centerX.mas_equalTo(self.bgView.mas_centerX);
            make.width.height.mas_equalTo(px1920Height(260));
        }];
        
        [self.textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.bgView.mas_left).mas_offset(px1080Width(43));
            make.right.mas_equalTo(self.bgView.mas_right).mas_offset(-px1080Width(43));
            make.top.mas_equalTo(self.imageView.mas_bottom).mas_offset(px1920Height(20));
            make.height.mas_equalTo(px1920Height(220));
        }];
    }

    
}



@end
